package com.kyzh.core.pager.me.settings

import android.app.AlertDialog
import android.os.Bundle
import android.text.InputType
import android.widget.EditText
import com.gushenge.core.dao.MMKVConsts
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.toast
import kotlinx.android.synthetic.main.activity_download_settings.*

class DownloadSettingsActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_download_settings)

        initView()
    }

    private fun initView() {
        select_net.isChecked = MMKVConsts.selectNetwork
        select_delete.isChecked = MMKVConsts.selectDeleteFile
        select_net.setOnCheckedChangeListener { _, b -> MMKVConsts.selectNetwork = b }
        select_delete.setOnCheckedChangeListener { _, b -> MMKVConsts.selectDeleteFile = b }
        select_download_count.text = String.format(getString(R.string.giftNum,MMKVConsts.selectDownloadNumber.toString()))
        select_download_count_root.setOnClickListener {inputCount() }

        select_download_dir.text = MMKVConsts.downloadDir.substring(19)
    }

    /*输入下载数量*/
    private fun inputCount() {
        val editText = EditText(this)
        editText.inputType = InputType.TYPE_CLASS_NUMBER
        AlertDialog.Builder(this)
                .setTitle(getString(R.string.pleaseInputDownloadNum))
                .setView(editText)
                .setPositiveButton(getString(R.string.sure)) { dialog, which ->
                    if (editText.text.toString().trim().isEmpty()) {
                        getString(R.string.numberIsNotEmpty).toast()
                    } else {
                        val count = Integer.parseInt(editText.text.toString().trim { it <= ' ' })
                        when {
                            count < 1 -> getString(R.string.numberMustBePositiveInteger).toast()
                            count > 5 -> getString(R.string.numberNotMoreThanFive).toast()
                            else -> {
                                select_download_count.text = String.format(getString(R.string.giftNum),count.toString())
                                MMKVConsts.selectDownloadNumber = count
                                getString(R.string.restartingAppToSave).toast()
                            }
                        }
                    }
                }
                .setNegativeButton(getString(R.string.cancel)) { dialog, _ -> dialog.dismiss() }
                .show()
    }
}